package com.keydom.ropeskipping.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import androidx.viewpager.widget.ViewPager;

public class LyViewPager extends ViewPager {
    private int currentIndex = 1;
    private float lastX;

    public LyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LyViewPager(Context context) {
        super(context);
    }

    public int getCurrentIndex() {
        return currentIndex;
    }

    public void setCurrentIndex(int currentIndex) {
        this.currentIndex = currentIndex;
    }

    @Override
    public void scrollTo(int x, int y) {
        super.scrollTo(x, y);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //如果滑动不做处理不滑动返回false，不消费给父控件去处理
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                float currentX = event.getX();
                float deltaX = currentX - lastX;
                if (deltaX > 0) {
                    // 右滑
                    Log.e("TAG", "onTouchEvent: 右滑");
                    if (currentIndex == 1) {
                        return false;
                    }
                } else if (deltaX < 0) {
                    // 左滑
                    Log.e("TAG", "onTouchEvent: 左滑");
                    if (currentIndex == -1) {
                        return false;
                    }
                }
                lastX = currentX;
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        //如果滑动不做处理不滑动返回false，不消费给父控件去处理
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                float currentX = event.getX();
                float deltaX = currentX - lastX;
                if (deltaX > 0) {
                    // 右滑
                    Log.e("TAG", "onInterceptTouchEvent: 右滑");
                    if (currentIndex == 1) {
                        return false;
                    }
                } else if (deltaX < 0) {
                    // 左滑
                    Log.e("TAG", "onInterceptTouchEvent: 左滑");
                    if (currentIndex == -1) {
                        return false;
                    }
                }
                lastX = currentX;
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.onInterceptTouchEvent(event);
    }

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        super.setCurrentItem(item, smoothScroll);
    }

    @Override
    public void setCurrentItem(int item) {
        super.setCurrentItem(item);
    }
}